package com.nbicc.dp.wsgateway.service;

import com.nbicc.core.utils.ExceptionUtil;
import com.nbicc.core.utils.ToolUtils;
import com.nbicc.db.modular.dp.panel.mapper.PanelMapper;
import com.nbicc.db.modular.dp.panel.model.Panel;
import com.nbicc.db.modular.dp.panelwidgetinstance.mapper.PanelWidgetInstanceMapper;
import com.nbicc.db.modular.dp.panelwidgetinstance.model.PanelWidgetInstance;
import com.nbicc.db.modular.dp.widgetinstance.mapper.WidgetInstanceMapper;
import com.nbicc.db.modular.dp.widgetinstance.model.WidgetInstance;
import com.nbicc.dp.wsgateway.holder.PanelHolder;
import com.nbicc.dp.wsgateway.model.WsPanel;
import com.nbicc.dp.wsgateway.model.WsWidgetInstance;
import com.nbicc.service.modular.dp.panel.service.PanelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.net.URI;
import java.util.*;

/**
 * @Author: lilingyan
 */
@Slf4j
@Service
public class PanelOnOffService {

    @Value("${wsgateway.panelTag}")
    private String panelTag = "panelSerialNum";

    @Autowired
    private PanelHolder panelHolder;
    @Autowired
    private PanelMapper panelMapper;
    @Autowired
    private PanelWidgetInstanceMapper panelWidgetInstanceMapper;

    public String getPanelIdByUri(URI uri){
        Map<String, String> splitUriQuery = ToolUtils.splitUriQuery(uri.getQuery());
        return splitUriQuery.get(panelTag);
    }

    public void comeOnline(String panelSerialNum , WebSocketSession session) throws IOException {
        try {
            Panel panel = panelMapper.selectBySerialNum(panelSerialNum);
            List<PanelWidgetInstance> panelWidgetInstanceList = panelWidgetInstanceMapper.listByPanelId(panel.getId());
            WsPanel wsPanel = new WsPanel();
            wsPanel.setPanelId(panel.getId());
            wsPanel.setPanelSerialNum(panelSerialNum);
            wsPanel.setSession(session);
            wsPanel.setWsWidgetInstanceSet(widgetInstance2WsWidgetInstance(panelWidgetInstanceList));
            panelHolder.addWsPanel(panelSerialNum , wsPanel);
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTraceInfo(e));
            if(session.isOpen()){
                session.close();
            }
        }
    }

    public void offline(WebSocketSession session) {
        panelHolder.removeByPanelSession(session);
    }

    private Set<WsWidgetInstance> widgetInstance2WsWidgetInstance(List<PanelWidgetInstance> panelWidgetInstanceList){
        Set<WsWidgetInstance> wsWidgetInstanceSet = new HashSet<>();
        if(panelWidgetInstanceList != null){
            for (PanelWidgetInstance panelWidgetInstance : panelWidgetInstanceList) {
                WsWidgetInstance wsWidgetInstance = new WsWidgetInstance();
                wsWidgetInstance.setWidgetInstanceId(panelWidgetInstance.getWidgetInstanceId());
                wsWidgetInstanceSet.add(wsWidgetInstance);
            }
        }
        return wsWidgetInstanceSet;
    }

}
